import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/view/user/Login'
import LoginPhone from '@/view/user/LoginPhone'
import Register from '@/view/user/Register'
import Index from '@/view/Index'
import ApplyHistory from '@/view/apply/ApplyHistory';
import Apply from '@/view/apply/Apply';
import Mess from '@/view/message/Message';
import Classroom from '@/view/classroom/Classroom';
import ClassroomDetails from '@/view/classroom/ClassroomDetails';
import Header from '@/view/setting/Header';
import Footer from '@/view/setting/Footer';
import Notice from '@/view/notice/Notice';
import Course from '@/view/course/Course';
import Welcome from '@/view/welcome/Welcome';
import NoticeDetails from '@/view/notice/NoticeDetails';

import { Message } from 'element-ui';
Vue.use(Router)

const router = new Router({
    routes: [{ //渲染到app.vue中 <router-view></router-view>
        path: '/login',
        component: Login
    }, {
        path: '/',
        redirect: '/welcome'
    }, {
        path: '/register',
        component: Register
    }, {
        path: '/loginPhone',
        component: LoginPhone
    }, {
        path: '/index',
        component: Index, //渲染到当前路由中 <router-view></router-view>
        children: [{
            path: '/applyHistory',
            component: ApplyHistory
        }, {
            path: '/apply',
            component: Apply
        }, {
            path: '/message',
            component: Mess
        }, {
            path: '/classroom',
            component: Classroom
        }, {
            path: '/notice',
            component: Notice
        }, {
            path: '/course',
            component: Course
        }, {
            path: '/welcome',
            component: Welcome
        }, {
            path: '/noticeDetails',
            component: NoticeDetails
        }, {
            path: '/classroomDetails',
            component: ClassroomDetails
        }]
    }, {
        path: '/header',
        component: Header,

    }, {
        path: '/footer',
        component: Footer
    }]
})

// 全局守卫，控制访问不同的页面需要的权限验证等信息
router.beforeEach((to, from, next) => {
    // 获取tokee
    const token = sessionStorage.getItem("USER-TOKEN");
    if (to.path == '/applyHistory' && !token) {
        // 提示，引导到登录界面中
        Message.warning('请先登录')
        next('/login');
    } else if (to.path == '/message' && !token) {
        Message.warning('请先登录')
        next('/login');
    } else if (to.path == '/classroom' && !token) {
        Message.warning('请先登录')
        next('/login');
    } else if (to.path == '/apply' && !token) {
        Message.warning('请先登录')
        next('/login');
    } else {
        // 放行
        next()
    }

})

export default router